package com.yz.jdd.module.psd.presenter;

import android.text.TextUtils;

import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.module.psd.view.ForgetPsdView;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Requset_usercenter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by cc on 2017/5/23.
 */

public class ForgetPsdPresenter {



    public ForgetPsdView forgetPsdView;
    Requset_usercenter requset_usercenter;

    public ForgetPsdPresenter(ForgetPsdView forgetPsdView) {
        this.forgetPsdView = forgetPsdView;
        requset_usercenter = new Requset_usercenter();
    }

    /**
     * 修改手机号
     */
    public void modifyAccount() {
        final String phoneNo = forgetPsdView.getPhoneNumber();

        if (TextUtils.isEmpty(phoneNo)) {
            forgetPsdView.showToastDialog("请输入绑定的手机号码");
        } else {
            requset_usercenter.setPhoneNum(phoneNo);
            String code = forgetPsdView.getCode();
            if (TextUtils.isEmpty(code)) {
                forgetPsdView.showToastDialog("请输入验证码");
            } else {
                forgetPsdView.showLoading("正在修改");
                requset_usercenter.setVerifyCode(code);
                NetWorkRequset.modifyPhone(requset_usercenter, new MySubscriber() {
                    @Override
                    public void onSuccess(Response_Base response_baseModel) {
                        forgetPsdView.dismissLoading();
                        forgetPsdView.showToastDialog(response_baseModel.getMsg());
                        forgetPsdView.colseActivity(phoneNo);
                    }

                    @Override
                    public void onNeedLogin(String msg) {
                        forgetPsdView.dismissLoading();
                        forgetPsdView.startLoginActivity();
                    }

                    @Override
                    public void onFail(Response_Base response_baseModel) {
                        forgetPsdView.dismissLoading();
                        forgetPsdView.showToastDialog(response_baseModel.getMsg());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        forgetPsdView.dismissLoading();
                    }
                });
            }

        }

    }

    /**
     * 验证验证码，根据action 判断
     * 是
     * 先验证 验证码成功，后打开修改密码界面
     * 或
     * 先验证验证码成功，然后直接调用修改账号接口
     */
    public void verifyCode(final String action) {
        final String phoneNo = forgetPsdView.getPhoneNumber();
        String code = forgetPsdView.getCode();
        Requset_usercenter requset_usercenter =new Requset_usercenter();
        if (TextUtils.isEmpty(phoneNo)) {
            forgetPsdView.showToastDialog("请输入绑定的手机号码");
        }else{
            requset_usercenter.setPhoneNum(phoneNo);
            if (TextUtils.isEmpty(code)) {
                forgetPsdView.showToastDialog("请输入验证码");
            } else {
                forgetPsdView.showLoading("正在验证");


                requset_usercenter.setVerifyCode(code);
                NetWorkRequset.verifyCode(requset_usercenter, new MySubscriber() {
                    @Override
                    public void onSuccess(Response_Base response_baseModel) {
                        forgetPsdView.dismissLoading();
                        if ("forget".equals(action)) {
                            forgetPsdView.startModifyPsdActivity(phoneNo);
                        } else {
                            modifyAccount();
                        }
                    }

                    @Override
                    public void onNeedLogin(String msg) {
                        forgetPsdView.dismissLoading();
                        forgetPsdView.startLoginActivity();
                    }

                    @Override
                    public void onFail(Response_Base response_baseModel) {
                        forgetPsdView.dismissLoading();
                        forgetPsdView.showToastDialog(response_baseModel.getMsg());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        forgetPsdView.dismissLoading();
                    }
                });
            }
        }


    }

    /**
     * 获取验证码
     * 重置code=1
     * 修改code=2
     */
    public void getVerifyCode(String codeType) {
        String phoneNumber = forgetPsdView.getPhoneNumber();


        if (TextUtils.isEmpty(phoneNumber)) {
            forgetPsdView.showToastDialog("请输入绑定的手机号码");
        } else {

            String phone="^1[0-9]\\d{9}$" ;
            Pattern pattern = Pattern.compile(phone);
            Matcher matcher = pattern.matcher(phoneNumber);


            if(matcher.matches()) {
                forgetPsdView.showLoading("正在加载");
                Requset_usercenter requset_sendVerifyCode=new Requset_usercenter();
//                requset_sendVerifyCode.setVerifyCodeType(codeType);
                requset_sendVerifyCode.setPhoneNum(phoneNumber);
                if("1".equals(codeType)){
                    NetWorkRequset.sendVerifyCodeResetPWD(requset_sendVerifyCode, new MySubscriber() {
                        @Override
                        public void onSuccess(Response_Base response_baseModel) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.startTimer();
                            if(!TextUtils.isEmpty(response_baseModel.getMsg()))
                                forgetPsdView.showToastDialog(response_baseModel.getMsg());
                        }

                        @Override
                        public void onNeedLogin(String msg) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.startLoginActivity();
                        }

                        @Override
                        public void onFail(Response_Base response_baseModel) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.showToastDialog(response_baseModel.getMsg());
                        }

                    });
                }else if("2".equals(codeType)){
                    NetWorkRequset.sendVerifyCodeModifyPhone(requset_sendVerifyCode, new MySubscriber() {
                        @Override
                        public void onSuccess(Response_Base response_baseModel) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.startTimer();
                            if(!TextUtils.isEmpty(response_baseModel.getMsg()))
                                forgetPsdView.showToastDialog(response_baseModel.getMsg());
                        }

                        @Override
                        public void onNeedLogin(String msg) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.startLoginActivity();
                        }

                        @Override
                        public void onFail(Response_Base response_baseModel) {
                            forgetPsdView.dismissLoading();
                            forgetPsdView.showToastDialog(response_baseModel.getMsg());
                        }

                    });
                }
            }else{
                forgetPsdView.showToastDialog("请输入正确手机号码");
            }


        }


    }


}
